package com.changil.admin.action.user;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.changil.admin.action.Action;
import com.changil.admin.dto.User;
import com.changil.admin.protocol.UserManager;
import com.changil.admin.protocol.http.HttpUtil;
import com.changil.admin.util.PasswordUtil;

public class LoginAction extends Action {

	@Override
	public void execute(final HttpServletRequest request,
			final HttpServletResponse response) throws IOException,
			ServletException {
		String id = request.getParameter("id");
		String password = PasswordUtil.PASSWORD(request.getParameter("password"));
		User user = UserManager.getsInstance().login(id, password);
		System.out.println("유저는 "+user);
		if (null != user) {
			HttpSession session = request.getSession(true);
			session.setAttribute("authUser", user);
			HttpUtil.redirect(response, "/NowonChangilChurch_Admin/main");
		} else {
			HttpUtil.redirect(response,
					"/NowonChangilChurch_Admin/user?a=loginform&result=fail");
		}

	}

}
